package com.eqdd.yiqidian.ui.gongneng.renshi.kaoqin.rule;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RuleListActivityCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.RuleListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class RuleListActivity extends BaseActivity {
    private RuleListActivityCustom dataBinding;
    private SlimAdapterEx<RuleListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_rule_list);
        initTopTitleBar(View.VISIBLE, "班别列表");
        initTopRightText("新建", v -> {
            startActivityForResult(AddRuleActivity.class, null, Config.ADD_RULE);
        });
    }

    @Override
    public void initData() {
        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
            refresh();
        });
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_RULE)
                    .params("ruleShiftId", slimAdapterEx.getDataItem(pos).getId())
                    .params("userGuid", user.getGuid())
                    .params("companyId", user.getCompanyId())
                    .execute(new DialogCallBack<HttpResult>(RuleListActivity.this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                menuBridge.closeMenu();
                                slimAdapterEx.getData().remove(pos);
                                slimAdapterEx.notifyItemRemoved(pos);
                            }
                        }
                    });
        });
        dataBinding.recyclerView.setSwipeItemClickListener((view, pos) -> {
            Bundle bundle = new Bundle();
            bundle.putParcelable("bean", slimAdapterEx.getDataItem(pos));
            startActivityForResult(UpdateRuleActivity.class, bundle, pos);
        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_45, new SlimInjector<RuleListResultBean>() {
            @Override
            public void onInject(RuleListResultBean data, IViewInjector injector) {

                injector.text(R.id.tv_upper_content, data.getRuleName() + "-" + data.getShiftName())
                        .text(R.id.tv_down_content, data.getWeeks() + " " + data.getHolidays());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.coolRefreshView.setRefreshing(true);
    }

    @Override
    public void setView() {

    }

    private void refresh() {
        OkGo.<HttpResult<List<RuleListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.RULE_LIST)
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<List<RuleListResultBean>>>(RuleListActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<RuleListResultBean>>> response) {
                        HttpResult<List<RuleListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());

                        }
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            if (requestCode == Config.ADD_RULE) {
                dataBinding.coolRefreshView.setRefreshing(true);
            } else {
                slimAdapterEx.getData().remove(requestCode);
                slimAdapterEx.getData().add(requestCode, data.getParcelableExtra("bean"));
                slimAdapterEx.notifyItemChanged(requestCode);
            }
        }
    }
}
